﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Resources;
using SIG.Model.Store;

namespace SIG.Service
{
    public class OrderService :  EntityService<Order>, IOrderService
    {
       
        private readonly IOrderRepository _orderRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public OrderService(IOrderRepository orderRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(orderRepository, cacheService, loggingService)
        {           
            _orderRepository = orderRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        //public void CreateOrder(Order order, List<Cart> cartItems, out decimal orderTotal)
        //{
        //    foreach (var item in cartItems)
        //    {
        //        var orderDetail = new OrderDetail
        //        {
        //            GoodsId = item.GoodsId,
        //            OrderId = order.Id,
        //            UnitPrice = item.Goods.Price,
        //            Quantity = item.Count
        //        };
        //        // Set the order total of the shopping cart
        //        orderTotal += (item.Count * item.Goods.Price);

        //        _orderDetailService.Create(orderDetail);

        //    }
        //    // Set the order's total to the orderTotal count
        //    order.Total = orderTotal;

        //    // Save the order
        //    storeDB.SaveChanges();
        //}


        //public List<Order> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        //{
        //    if (string.IsNullOrEmpty(keyword))
        //    { 
        //        return GetPagedElements(pageIndex, pageSize, out totalCount);
        //    }

        //    //get list count
        //    var keyCount = $"{EntityNames.Order}_ListCount__{keyword}";            
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(keyCount))
        //        {
        //            totalCount = (int)_cacheService.Get(keyCount);

        //        }
        //        else
        //        {
        //            totalCount = _orderRepository.GetAllElements().Where(c=>c.Title.Contains(keyword)).Count();
        //            _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        totalCount = _orderRepository.GetAllElements().Where(c => c.Title.Contains(keyword)).Count();
        //    }


        //    //get list
        //    var key = $"{EntityNames.Order}_List_{pageIndex}_{pageSize}_{keyword}";

        //    List<Order> categories;
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(key))
        //        {
        //            categories = (List<Order>)_cacheService.Get(key);

        //        }
        //        else
        //        {
        //            categories = _orderRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), (c => c.Title.Contains(keyword)), false).ToList();
        //            _cacheService.Set(key, categories, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        categories = _orderRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), (c => c.Title.Contains(keyword)), false).ToList();
        //    }

        //    return categories;            

        //}

        //public List<Order> GetPagedElements(int pageIndex, int pageSize, out int totalCount)
        //{

        //    //get list count
        //    var keyCount = $"{EntityNames.Order}_ListCount";
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(keyCount))
        //        {
        //            totalCount = (int)_cacheService.Get(keyCount);

        //        }
        //        else
        //        {
        //            totalCount = _orderRepository.GetAllElements().Count();
        //            _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        totalCount = _orderRepository.GetAllElements().Count();
        //    }


        //    //get list
        //    var key = $"{EntityNames.Order}_List_{pageIndex}_{pageSize}";

        //    List<Order> categories;
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(key))
        //        {
        //            categories = (List<Order>)_cacheService.Get(key);

        //        }
        //        else
        //        {
        //            categories = _orderRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), false).ToList();
        //            _cacheService.Set(key, categories, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        categories = _orderRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), false).ToList();
        //    }

        //    return categories;

        //}
    }
}
